package com.example.fp.cgc.util

import com.example.fp.cgc.connerlt.AlarmTask
import com.example.fp.cgc.controller.NoticeController
import com.example.fp.cgc.entity.UserSetting
import com.google.gson.Gson
import java.util.*

/**
 * @author 蔡
 * @since 2020-02-25
 */
object DataUtil {
    data class HistoryData(
            var uid: Int,
            var text: String
    )

    fun getUserHistoryList(uid: Int): String {
        val mapper = AlarmTask.task.messageMapper
        val historyUser = mapper.getHistoryUser(uid)
        val mData = mutableListOf<HistoryData>()
        historyUser.forEach {
            mData.add(HistoryData(it, mapper.getLastMessage(uid, it)[0].messageText))
        }

        return getData("historyList", Gson().toJson(mData))
    }

    data class FollowUser(
            var uid: Int
    )

    fun getFollowList(uid: Int): String {
        val mapper = AlarmTask.task.userFollowMapper
        val followList = mapper.selectFollowList(uid)

        val mData = mutableListOf<FollowUser>()
        followList.forEach {
            mData.add(FollowUser(it.oid))
        }

        return getData("followList", Gson().toJson(mData))
    }

    fun getSettingState(uid: Int): String {
        val mapper = AlarmTask.task.userSettingMapper
        var setting = mapper.getState(uid)

        if (setting.isEmpty()) {
            mapper.insert(UserSetting(uid, offline = true, history = true, unread = true))
            setting = mapper.getState(uid)
        }

        return getData("setting", Gson().toJson(setting[0]))
    }

    /**
     * 2020-02-28
     */
    fun getUserOnlineState(): String {
        var da = "{"
        da += """
            "type": [{
                "type" : "useronline"
              }],
        """.trimIndent()
        da += AlarmTask.task.dataStateMap["useronline"]?.data
        da += AlarmTask.task.getJSONData("tokendata")
        da += "}"


        return da
    }

    data class boxData(
            var id: Int,
            var text: String,
            var time: Date
    )

    /**
     * @since 2020-03-01
     */
    fun getMessageBox(uid: Int): String {
        val mapper = AlarmTask.task.messageMapper
        val historyUser = mapper.getHistoryUserUnread(uid)
        val mData = mutableListOf<boxData>()
        historyUser.forEach {
            val now = Date()

            val message = mapper.getLastMessage(uid, it)[0]
            val time = Date(now.time - message.messageDate!!.time)
            mData.add(boxData(it, message.messageText, time))
        }

        return getData("messageBox", Gson().toJson(mData))
    }

    /**
     * @since 2020-03-06
     */
    fun getNoticeBox(uid: Int,flag:Boolean): String {
        val mapper = NoticeController.noticeController.noticeMapper
        val noticeData = mapper.getUnreadNotice(uid)
        mapper.setNoticeRead(uid)
        return if(flag){
            getData("noticeList", Gson().toJson(noticeData))
        }else{
            getData("notice", Gson().toJson(noticeData))
        }

    }


    fun getData(type: String, json: String): String {
        val str =
                """
                    "$type":${json},
                """.trimIndent()
        var da = "{"
        da += """
            "type": [{
                "type" : "$type"
              }],
        """.trimIndent()

        da += str
        da += AlarmTask.task.getJSONData("tokendata")
        da += "}"
        return da
    }
}